package com.kzj.mls.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kzj.mls.R;
import com.kzj.mls.beans.CloseActivityBean;
import com.kzj.mls.beans.OrderBean;
import com.kzj.mls.beans.OrderDetailsBean;
import com.kzj.mls.beans.ServiceListBean;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.OnButtonPressListener;
import com.kzj.mls.recyclerCard.basic.SuperSwipeRefresh;
import com.kzj.mls.recyclerCard.card.Card;
import com.kzj.mls.recyclerCard.card.other.OrderCard;
import com.kzj.mls.recyclerCard.card.other.OrderDetailsCard;
import com.kzj.mls.recyclerCard.card.other.ServiceCard;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.kzj.mls.widget.dialog.SweetDialogManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/3.
 */
public class OrderDetailsActivity extends GBaseActivity {
    private MaterialListView material_listview;
    private String order_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_superswiperefresh_materlistview);
        findViewByIds();
        initDatas();
        fillArrayInListView();
        TaskGetOrderDetails(order_id);
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);

    }

    @Override
    protected void initDatas() {
        setTopTitle("订单详情");
        setLeftImg(View.VISIBLE, R.drawable.back);
        order_id = getIntent().getStringExtra("order_id");
    }

    @Override
    protected void fillArrayInListView() {

    }

    private void fillArrayInList(OrderDetailsBean orderDetailsBean) {
        OrderDetailsCard orderDetailsCard = new OrderDetailsCard(this);
        orderDetailsCard.setOrderDetails(orderDetailsBean);
        orderDetailsCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
            @Override
            public void onButtonPressedListener(View view, final Card card) {
                switch (view.getId()) {
                    case R.id.textView_complete:
                        SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) getActivity(), "是否完成订单？",
                                "", true, true, new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismiss();
                                    }
                                }, new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        TaskCompleteOrder(((OrderDetailsCard) card).getOrderDetails().getOrder_id());
                                    }
                                });
                        break;
                    case R.id.textView_sure:
                        SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) getActivity(), "是否确认订单？",
                                "", true, true, new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismiss();
                                    }
                                }, new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        TaskConfirmOrder(((OrderDetailsCard) card).getOrderDetails().getOrder_id());
                                    }
                                });
                        break;
                    case R.id.textView_call:
                        SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) getActivity(), "拨打电话",
                                ((OrderDetailsCard) card).getOrderDetails().getPhone_mob(), true, true, new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismiss();
                                    }
                                }, new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismiss();
                                        Intent intent6 = new Intent(Intent.ACTION_DIAL,
                                                Uri.parse("tel:" + ((OrderDetailsCard) card).getOrderDetails().getPhone_mob()));
                                        getActivity().startActivity(intent6);
                                    }
                                });
                        break;

                }
            }
        });
        material_listview.add(orderDetailsCard);
    }

    private void TaskConfirmOrder(String order_id) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Confirm_Order, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Confirm_Order, order_id), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        App.getInstance().showToast("已确认订单！");
                        OrderDetailsActivity.this.finish();
                        EventBus.getDefault().post(new CloseActivityBean());
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    private void TaskCompleteOrder(String order_id) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Complete_Order, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Complete_Order, order_id), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        App.getInstance().showToast("订单确认完成！");
                        OrderDetailsActivity.this.finish();
                        EventBus.getDefault().post(new CloseActivityBean());
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    private void TaskGetOrderDetails(String id) {
        RequestServerManager.getInstance().handleMethod(this, null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Order_Details, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Order_Details, id), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        OrderDetailsBean orderDetailsBean = JSON.parseObject(result.getRetval().toString(), OrderDetailsBean.class);
                        fillArrayInList(orderDetailsBean);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");

                    }
                });
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
